package com.mxprojects.gui
{
	import com.mxprojects.constructors.ButtonHighlight;
	import com.mxprojects.constructors.Divider;
	import com.mxprojects.constructors.DrawBorder;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.ProfileWindow;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class ThreadItem extends BaseForumElement
	{
		private const DIVIDER_COUNT:int = 4;
		
		public var topic:TextField;
		public var topicStarter:TextField;
		public var replies:TextField;
		public var views:TextField;
		public var lastPost:TextField;
				
		//setup the vars to hold on to stuff for mouse clicks and junk
		public var threadID:int;
		private var threadLocked:int;
		private var threadSticky:int;
		private var threadStarter:String;
		private var threadStarterID:int;
		
		//stuff to share
		private var threadHeader:ThreadHeader;
		private var mainButton:ButtonHighlight;
		private var profileButton:ButtonHighlight;
		
		public function ThreadItem(topic:String, topicStarter:String, topicStarterID:int, replies:String, views:String, lastPost:String, lastUser:String, threadHeader:ThreadHeader){
			//set the values from CSS
			this.topic.styleSheet = Styles.css;
			this.topicStarter.styleSheet = Styles.css;
			this.replies.styleSheet = Styles.css;
			this.views.styleSheet = Styles.css;
			this.lastPost.styleSheet = Styles.css;
			
			//setup the textfields
			this.topic.htmlText = "<span class=\"threadTopic\">" + topic + "</span>";
			this.topicStarter.htmlText = "<span class=\"threadTopicStarter\">" + topicStarter + "</span>";
			this.replies.htmlText = "<span class=\"threadReplies\">" + replies + "</span>";
			this.views.htmlText = "<span class=\"threadViews\">" + views + "</span>";
			this.lastPost.htmlText = "<span class=\"threadLastPost\">" + lastPost + "<br>" + Language.main(Word.BY, false, true) + " " + lastUser + "</span>";
			
			//share the header
			this.threadHeader = threadHeader;
			
			//set the shared vars
			threadStarter = topicStarter;
			threadStarterID = topicStarterID;
			
			//add a couple of buttons to click
			mainButton = new ButtonHighlight(this, viewPost);
			addChild(mainButton);
			
			profileButton = new ButtonHighlight(this, viewProfile);
			addChild(profileButton);
			
			//parse the CSS
			super.init(".threadBackground", DIVIDER_COUNT);
			
			//place the stuff
			//placeStuff();
		}
		
		public function setProperties(id:int, isThreadLocked:int, isThreadSticky:int, lastRead:int, announcementDisplayNumber:int):void {
			threadID = id;
			threadLocked = isThreadLocked;
			threadSticky = isThreadSticky;
			
			//attach a forum status icon and put it where it needs to go
			var statusIcon:StatusIcon = new StatusIcon(StatusIcon.OLD);
			
			//check if we have some new posts. -1 means we don't
			if(lastRead >= 0){
				statusIcon.setStatus(StatusIcon.NEW);
			}
			statusIcon.x = Config.TEXT_PADDING;
			statusIcon.y = Math.round((height/2) - (statusIcon.height/2));
			addChildAt(statusIcon, getChildIndex(mainButton)-1);
			
			//move the title and description
			topic.x = statusIcon.x + statusIcon.width + Config.TEXT_PADDING;
			
			//if the forum is read only let's put a lock on it
			if(isThreadLocked == 1){
				var lock:StatusIcon = new StatusIcon(StatusIcon.LOCKED);
				lock.x = statusIcon.x;
				lock.y = statusIcon.y;
				addChildAt(lock, getChildIndex(statusIcon)+1);
			}
			
			if(isThreadSticky == 1){
				//check the "Sticky" status
				topic.htmlText = Language.main(Word.STICKY) + ": " + topic.htmlText;
				
				var sticky:StatusIcon = new StatusIcon(StatusIcon.STICKY);
				sticky.x = statusIcon.x;
				sticky.y = statusIcon.y;
				addChildAt(sticky, getChildIndex(statusIcon)+1);
			}else if(isThreadSticky == announcementDisplayNumber){
				topic.htmlText = Language.main(Word.ANNOUCEMENT) + ": " + topic.htmlText;
			}
		}
		
		override public function resize():void {
			placeStuff();
			
			super.resize();
		}
		
		private function placeStuff():void {
			//text boxes
			topic.width = threadHeader.getDivider(0).x - topic.x;
			topicStarter.x = threadHeader.topicStarter.x;
			replies.x = threadHeader.replies.x;
			views.x = threadHeader.views.x;
			lastPost.x = threadHeader.lastPost.x;
			
			//dividers
			for(var i:uint = 0; i < DIVIDER_COUNT; i++){
				getDivider(i).x = threadHeader.getDivider(i).x;
			}
			
			//buttons
			mainButton.width = this.width;
			profileButton.x = threadHeader.getDivider(0).x + threadHeader.getDivider(0).width;
			profileButton.width = topicStarter.width + Config.TEXT_PADDING - threadHeader.getDivider(1).width;
		}
		
		private function viewPost(event:MouseEvent):void {
			DeepLinking.setLink(Word.POSTS, DeepLinking.createLinkArray(threadID));
			//Posts.load(threadID);
		}
		
		private function viewProfile(event:MouseEvent):void {
			//pop a window of the user's profile
			ProfileWindow.view(threadStarter, threadStarterID);			
		}
		
	}
}